package com.syxp.pda.action.detail;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.util.Operation;
import com.syxp.pda.core.util.TimeFormatUtil;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.detail.CheckPlanDetail;
import com.syxp.pda.entity.detail.CheckPlanDetailDTO;
import com.syxp.pda.service.detail.ExceptionService;

public class ExceptionAction extends BaseAction {

	@Resource
	private ExceptionService exceptionService;

	public ExceptionService getExceptionService() {
		return exceptionService;
	}

	public void setExceptionService(ExceptionService exceptionService) {
		this.exceptionService = exceptionService;
	}

	private CheckPlanDetailDTO planDetail;

	public CheckPlanDetailDTO getPlanDetail() {
		return planDetail;
	}

	public void setPlanDetail(CheckPlanDetailDTO planDetail) {
		this.planDetail = planDetail;
	}

	public String get() {
		try {
			List<FieldBean> fields = buildFieldBean(planDetail);
			QueryFilter filter = new QueryFilter();
			for (FieldBean field : fields) {
				filter.addFilter(field);
			}
			List<CheckPlanDetail> details = exceptionService
					.selectException(filter);
			JsonObject jsonObj = new JsonObject();
			jsonObj.add("details", gson.toJsonTree(details));
			jsonObj.addProperty("success", true);
			this.jsonString = gson.toJson(jsonObj);
			System.out.println(jsonString);
		} catch (Exception e) {
			e.printStackTrace();
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}
	
	public String count() {
		try {
			List<FieldBean> fields = buildFieldBean(planDetail);
			QueryFilter filter = new QueryFilter();
			for (FieldBean field : fields) {
				filter.addFilter(field);
			}
			List<Object[]> details = exceptionService.exceptionCount(filter);
			JsonArray jarray = new JsonArray() ;
			for(Object[] objs : details) {
				JsonObject jobj = new JsonObject() ;
				jobj.addProperty("zoneName", String.valueOf(objs[0])) ;
				jobj.addProperty("devName", String.valueOf(objs[1])) ;
				jobj.addProperty("pda_starTime", String.valueOf(objs[2])) ;
				jobj.addProperty("pda_endTime", String.valueOf(objs[3])) ;
				jobj.addProperty("count", String.valueOf(objs[4])) ;
				jarray.add(jobj) ;
			}
			JsonObject jsonObj = new JsonObject();
			jsonObj.add("details", jarray);
			jsonObj.addProperty("success", true);
			this.jsonString = gson.toJson(jsonObj);
			System.out.println(jsonString);
		} catch (Exception e) {
			e.printStackTrace();
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}

	private List<FieldBean> buildFieldBean(CheckPlanDetailDTO o)
			throws IllegalArgumentException, IllegalAccessException,
			ParseException {
		Field[] fields = o.getClass().getDeclaredFields();
		Field.setAccessible(fields, true);
		List<FieldBean> qf = new ArrayList<FieldBean>();
		System.out.println(o);
		for (Field field : fields) {
			if (field.getType().getName().equals("java.lang.String")
					&& field.get(o) != null
					&& !((String) field.get(o)).equals("")) {
				if (field.getName().equals("startTime")) {
					qf.add(new FieldBean("start_PDA_CHKDateTime",
							TimeFormatUtil.parseDateF((String) field.get(o)),
							Operation.GE));
				} else if (field.getName().equals("endTime")) {
					qf.add(new FieldBean("end_PDA_CHKDateTime", TimeFormatUtil
							.parseDateF((String) field.get(o)), Operation.LE));
				} else {
					qf.add(new FieldBean(field.getName(), field.get(o),
							Operation.LIKE));
				}
			} else if (field.getType().getName().equals("int")
					&& field.getInt(o) != 0) {
				qf.add(new FieldBean(field.getName(), field.get(o),
						Operation.EQUAL));
			}
		}
		return qf;
	}
}
